1 00:00:00,750 --> 00:00:01,710 Welcome back. 2 00:00:01,710 --> 00:00:06,720 In this lecture we're going to be implementing the animations we've just created into our guns. 3 00:00:06,720 --> 00:00:10,260 So when we equip our gun we'll play the equip animation. 4 00:00:10,260 --> 00:00:13,710 When we shoot our gun we play the shoot animation. 5 00:00:13,710 --> 00:00:18,150 And when we reload we play the reload animation. 6 00:00:18,150 --> 00:00:19,770 So let's get started. 7 00:00:20,730 --> 00:00:26,310 You're most likely going to be using the animation rig that I've attached in the last lecture for your 8 00:00:26,310 --> 00:00:30,330 gun animations, but if you've created your own gun animations, then that's great too. 9 00:00:30,360 --> 00:00:36,060 Just make sure to open up your animation editor, select the rig and just go through and make sure you 10 00:00:36,060 --> 00:00:41,580 have all of the different animations for our pistol, our shotguns, our rifle, and our SMG. 11 00:00:41,610 --> 00:00:44,580 Make sure you have all of those animations uploaded. 12 00:00:44,610 --> 00:00:51,570 Then you can go into the module scripts for each one of your guns, and you make sure to update the 13 00:00:51,570 --> 00:00:56,460 animations with the corresponding IDs with the ones you've just uploaded. 14 00:00:56,460 --> 00:01:02,760 So for example, if I go to my toolbox and I go to my animations, I can go ahead and search for example 15 00:01:02,760 --> 00:01:08,700 for my shotgun and I've uploaded my shotgun animations, and then I've just copied the IDs and pasted 16 00:01:08,700 --> 00:01:11,850 them into the modules for my shotguns. 17 00:01:11,850 --> 00:01:16,920 I updated them right there, so make sure you have updated every single properties module script in 18 00:01:16,920 --> 00:01:20,790 your guns to match the animations that you've uploaded. 19 00:01:21,590 --> 00:01:25,040 Now we can go back into the local script that we've been editing so far. 20 00:01:25,040 --> 00:01:31,430 I'll open it up and in order to load animations onto our player's character, we of course need to get 21 00:01:31,430 --> 00:01:35,600 the character, the humanoid, and the animator inside of our humanoid. 22 00:01:35,810 --> 00:01:41,570 So I'll create a variable for my character that's going to be equal to my player dot character or player 23 00:01:41,570 --> 00:01:43,490 dot character added. 24 00:01:43,490 --> 00:01:47,450 We'll wait for that event to fire just in case we'll get the humanoid. 25 00:01:47,450 --> 00:01:49,160 So that's character. 26 00:01:49,990 --> 00:01:52,330 Wait for child humanoid. 27 00:01:52,330 --> 00:01:54,430 And then we can go ahead and get the animator. 28 00:01:54,430 --> 00:01:55,930 So humanoid. 29 00:01:55,930 --> 00:01:58,480 Wait for child animator. 30 00:01:58,990 --> 00:02:03,340 Then we can go ahead and create a new function to create animation tracks. 31 00:02:03,340 --> 00:02:06,490 So we'll call it Create Animation Track. 32 00:02:06,640 --> 00:02:12,280 Again, we'll get past an animation ID and we'll simply create a new animation instance. 33 00:02:15,450 --> 00:02:22,350 Set the animation ID equal to this anim id, and then we can go ahead and return from our animator. 34 00:02:22,500 --> 00:02:25,740 And we're going to load this animation. 35 00:02:27,610 --> 00:02:32,470 So then right here we can go ahead and create three variables one for our ship, one for our shoot and 36 00:02:32,470 --> 00:02:34,150 one for our reload animation. 37 00:02:34,150 --> 00:02:38,290 So we'll have the equip track that's going to be equal to create animation track. 38 00:02:38,290 --> 00:02:43,180 And we're going to pass properties dot animations dot equip. 39 00:02:44,960 --> 00:02:47,210 We can go ahead and get our shoot track. 40 00:02:47,940 --> 00:02:52,380 And we'll just copy this here, paste that there, and then get the shoe animation. 41 00:02:52,380 --> 00:02:55,200 And last but not least we'll copy this, paste that there. 42 00:02:55,200 --> 00:02:57,930 And this one's going to be for our load track. 43 00:02:57,930 --> 00:03:00,120 And we'll get the reload animation. 44 00:03:00,710 --> 00:03:04,160 Now we can go ahead and go to our equip event. 45 00:03:04,160 --> 00:03:08,150 And when we equip the gun, we want to play our equip animation. 46 00:03:08,150 --> 00:03:10,280 So equip track play. 47 00:03:10,760 --> 00:03:14,150 When we shoot our gun we want to play the shoot animation. 48 00:03:14,150 --> 00:03:20,390 So after we do all the calculations for the gun at the end here in my shoot function, we can play the 49 00:03:20,390 --> 00:03:21,380 shoot track. 50 00:03:22,850 --> 00:03:29,120 And then for the reload function, what we want to do is we want to replace this yield statement here 51 00:03:29,120 --> 00:03:30,260 with our animation. 52 00:03:30,260 --> 00:03:33,920 So we're going to refer to our reload track, play this track. 53 00:03:33,920 --> 00:03:40,760 And then we want to adjust the speed of this reload track to match inside of our properties the reload 54 00:03:40,760 --> 00:03:42,230 duration that we've set here. 55 00:03:42,680 --> 00:03:46,790 So reload track adjust speed. 56 00:03:46,790 --> 00:03:50,990 And it's going to be one divided by our properties dot reload duration. 57 00:03:51,380 --> 00:03:56,510 Then we can go ahead and wait for I believe, the ended event. 58 00:03:57,050 --> 00:04:01,160 For this animation track, and we can go ahead and get rid of this yield statement. 59 00:04:01,160 --> 00:04:06,560 And then once the animation track is finished playing, then we can go ahead and reload our gun. 60 00:04:07,480 --> 00:04:11,590 And now what we want to go ahead and do is we want to check. 61 00:04:12,890 --> 00:04:17,690 Or when we unequip our gun, we want to go ahead and stop playing all of the animations. 62 00:04:17,690 --> 00:04:22,280 So if we were in the middle of a reload animation and we unequip the gun, we want to stop playing the 63 00:04:22,280 --> 00:04:23,150 reload animation. 64 00:04:23,150 --> 00:04:28,040 We want to stop playing the equip animation, and we want to stop playing the shoot animation. 65 00:04:28,040 --> 00:04:29,120 So that's very easy. 66 00:04:29,120 --> 00:04:31,850 We'll just refer to our shoot track and stop it. 67 00:04:31,850 --> 00:04:38,900 We'll refer to our reload track and stop it, and we'll refer to our equip track and stop that as well. 68 00:04:39,530 --> 00:04:45,110 Now, something actually very important to note is that if I go back to my animation rig, let me open 69 00:04:45,110 --> 00:04:52,580 up my animation editor and I were to reload for the shoot track of my shotgun, let me find my shotgun 70 00:04:52,580 --> 00:04:53,690 shoot animation. 71 00:04:53,690 --> 00:04:55,310 So shotgun shoot animation. 72 00:04:56,140 --> 00:04:59,980 I have added this event right here called pump. 73 00:05:00,010 --> 00:05:06,250 It's an animation event, and we're going to use this animation event to play that shotgun pump sound 74 00:05:06,250 --> 00:05:08,260 that was in the properties for our shotgun. 75 00:05:08,260 --> 00:05:11,260 So for example, if we take a look at one of our shotguns here. 76 00:05:12,390 --> 00:05:16,050 When we scroll down, we have the shotgun pump sound properties. 77 00:05:16,050 --> 00:05:21,390 So for any of our guns that are shotguns, we're going to listen for this event inside of the animation 78 00:05:21,390 --> 00:05:21,690 track. 79 00:05:21,690 --> 00:05:26,730 And when we hit that event, we want to play that sound like we're wracking the shotgun. 80 00:05:26,730 --> 00:05:32,850 If you haven't added this event into your shotgun shoot animation, then go ahead and do so. 81 00:05:32,850 --> 00:05:37,530 You can just right click into your animation and you can press Add Animation Event. 82 00:05:38,280 --> 00:05:39,600 And then you'll add it. 83 00:05:39,600 --> 00:05:43,080 You'll give it a name, you'll call it pump, and that's it. 84 00:05:43,080 --> 00:05:44,040 That's all you need to do. 85 00:05:44,070 --> 00:05:49,500 You'll have your new animation track inside of your animation, and then you can go ahead and upload 86 00:05:49,500 --> 00:05:50,700 it to Roblox. 87 00:05:50,700 --> 00:05:55,650 So if you've already uploaded your shotgun shoe animation but you did not add this event, then go ahead 88 00:05:55,650 --> 00:06:01,200 and add this event into your shotgun shoe animation, and then make sure to publish and override the 89 00:06:01,200 --> 00:06:02,790 one you've previously uploaded. 90 00:06:02,790 --> 00:06:07,770 However, if you're using my animations, then you don't need to worry because my animations will already 91 00:06:07,770 --> 00:06:11,040 have this event included in the animation. 92 00:06:11,400 --> 00:06:18,390 So back in our local script, what we want to do is we want to check if our gun is a shotgun. 93 00:06:18,390 --> 00:06:26,100 So if gun get attribute gun type is equal to the spread or shotgun gun type, then what we can go ahead 94 00:06:26,100 --> 00:06:28,530 and do is refer to our shoot animation track. 95 00:06:28,530 --> 00:06:32,280 And we're going to use a function called get marker reach signal. 96 00:06:32,280 --> 00:06:36,780 Now the linter does not know what our shoot track variable is. 97 00:06:36,780 --> 00:06:42,120 So just to make it a little bit more clear, I'm going to signify that this function right here returns 98 00:06:42,120 --> 00:06:44,280 an animation track. 99 00:06:44,280 --> 00:06:46,950 Again, you do not need to worry about this type annotation. 100 00:06:46,950 --> 00:06:53,880 I'm just doing it for the lecture to make it easier for us to code, because now it will auto fill all 101 00:06:53,880 --> 00:06:55,980 of the stuff for my animation track. 102 00:06:55,980 --> 00:06:59,550 And the function we're looking for is the get marker reach signal. 103 00:06:59,550 --> 00:07:02,940 So this is where we pass the name of that event, which would be pump. 104 00:07:02,940 --> 00:07:09,390 And that means we're going to get an event that will be fired when we reach that event in our animation. 105 00:07:09,390 --> 00:07:11,250 So our event is called pump. 106 00:07:11,550 --> 00:07:14,400 And we can go ahead and connect a function to this. 107 00:07:14,970 --> 00:07:18,750 And what we want to do is we want to create a new sound from our create sound function. 108 00:07:18,750 --> 00:07:23,190 And that sound is going to be parented to our gun dot handle. 109 00:07:24,790 --> 00:07:28,210 And the properties is going to be the shotgun. 110 00:07:28,300 --> 00:07:33,220 So shotgun pump sound properties. 111 00:07:34,360 --> 00:07:36,850 Then we can go ahead and play the sound. 112 00:07:37,510 --> 00:07:43,270 And then when that sound ends, we'll go ahead and destroy the sound. 113 00:07:43,600 --> 00:07:48,610 And just like that, we should now have implemented all the animations into our gun. 114 00:07:48,610 --> 00:07:51,100 So to test this out, we can. 115 00:07:51,900 --> 00:07:57,270 Go and copy our assault rifle and paste it into the starter pack. 116 00:07:57,300 --> 00:08:00,930 We can also copy one of our shotguns and paste it in there. 117 00:08:00,930 --> 00:08:05,940 And let me make sure to put the newly updated local script in there and get rid of the old one. 118 00:08:06,360 --> 00:08:09,150 And then we could test another gun like our pistol. 119 00:08:09,150 --> 00:08:14,010 So I'll copy our pistol, paste that in there, and I'll make sure to copy the local script inside of 120 00:08:14,010 --> 00:08:14,850 it as well. 121 00:08:15,090 --> 00:08:17,250 And now we can go and play test our game. 122 00:08:18,350 --> 00:08:19,730 Now let me spawn in. 123 00:08:19,730 --> 00:08:22,220 And now if I equip my guns. 124 00:08:22,220 --> 00:08:26,810 As you can see, each of them has their equip animation track plane. 125 00:08:26,810 --> 00:08:33,470 If I shoot, as you can see, it's playing the shoot animation track and if I hold down, as you can 126 00:08:33,470 --> 00:08:36,380 see it is repeatedly playing that animation. 127 00:08:36,380 --> 00:08:42,080 Same thing with the shotgun if I shoot and there you go, you heard the pump sound because when I click, 128 00:08:42,920 --> 00:08:48,590 it reached that animation event inside of our animation and it's playing the racking sound for the shotgun. 129 00:08:49,430 --> 00:08:50,480 Very cool. 130 00:08:50,480 --> 00:08:52,970 And then for the pistol, let's go ahead and try it out. 131 00:08:55,280 --> 00:08:55,760 All right. 132 00:08:55,760 --> 00:08:56,600 That's working as well. 133 00:08:56,600 --> 00:08:58,100 Let's try reloading. 134 00:08:58,100 --> 00:08:59,420 So if I hit R. 135 00:09:01,310 --> 00:09:01,730 Here we go. 136 00:09:01,730 --> 00:09:05,120 Our reload track plays as well and we've reloaded our pistol. 137 00:09:05,120 --> 00:09:06,920 Let's go ahead and try it out for the shotgun. 138 00:09:09,890 --> 00:09:10,490 There you go. 139 00:09:10,490 --> 00:09:13,190 We're reloading our shotgun, and that works as well. 140 00:09:13,790 --> 00:09:15,080 Let's try the AR. 141 00:09:15,110 --> 00:09:16,040 Reload. 142 00:09:17,480 --> 00:09:18,050 And. 143 00:09:18,050 --> 00:09:18,530 Perfect. 144 00:09:18,530 --> 00:09:20,150 It's working just fine. 145 00:09:21,010 --> 00:09:25,810 And just like that, we're finished implementing the animations for our guns. 146 00:09:25,810 --> 00:09:28,090 I'll see you in the next lecture.